﻿using ChineseChess.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChineseChess.Controllers
{
    public class HomeController : Controller
    {
        public static Chess chess = new();
        public static bool playerRed = false;
        public static bool Continue = false;

        public static DateTime LastClick = DateTime.MinValue;

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost("Start")]
        /// <summary>
        /// 玩家点击开始
        /// </summary>
        /// <returns></returns>
        public async Task<Chess> Start()
        {
            StreamReader sr = new(Request.Body);
            string result = await sr.ReadToEndAsync();
            sr.Close();

            if (result == "0")
            {
                playerRed = true;
            }
            else
            {
                playerRed = false;
            }

            chess = new Chess();
            Continue = true;

            return chess;
        }

        [HttpPost("Click")]
        /// <summary>
        /// 用户点击某位置
        /// </summary>
        /// <returns></returns>
        public async Task<Chess> Click()
        {

            //响应时差应该大于200ms
            if ((DateTime.Now - LastClick) < new TimeSpan(0, 0, 0, 0, 200))
            {
                return null;
            }
            LastClick = DateTime.Now;

            if (!Continue)
            {
                return null;
            }

            StreamReader sr = new StreamReader(Request.Body);
            string result = await sr.ReadToEndAsync();
            sr.Close();

            Point point = JsonConvert.DeserializeObject<Point>(result);

            //玩家回合才响应点击
            if (chess.Count % 2 == 0 && playerRed || chess.Count % 2 == 1 && !playerRed)
            {
                if (chess.Selects.Count == 2)
                {
                    chess.Selects.Clear();
                }
                chess.Mays.Clear();

                List<int[]> allmoves = chess.AllMoves();

                if (chess.Selects.Count == 1)
                {
                    //是行棋还是换一个己方棋子点击
                    bool go = false;
                    foreach (var move in allmoves)
                    {
                        if (move[2] == point.X && move[3] == point.Y && move[0] == chess.Selects[0].X && move[1] == chess.Selects[0].Y)
                        {
                            go = true;
                            chess.Selects.Add(new Select() { X = point.X, Y = point.Y });
                            chess.Move(chess.Selects[0].X, chess.Selects[0].Y, chess.Selects[1].X, chess.Selects[1].Y);
                            if (chess.AllMoves().Count == 0)
                            {
                                Continue = false;
                            }
                            break;
                        }
                    }
                    if (!go)
                    {
                        chess.Selects.Clear();
                        foreach (var move in allmoves)
                        {
                            if (point.X == move[0] && point.Y == move[1])
                            {
                                if (chess.Selects.Count == 0)
                                {
                                    chess.Selects.Add(new Select() { X = point.X, Y = point.Y });
                                }
                                chess.Mays.Add(new May() { X = move[2], Y = move[3] });
                            }
                        }
                    }
                }
                else
                {
                    foreach (var move in allmoves)
                    {
                        if (point.X == move[0] && point.Y == move[1])
                        {
                            if (chess.Selects.Count == 0)
                            {
                                chess.Selects.Add(new Select() { X = point.X, Y = point.Y });
                            }
                            chess.Mays.Add(new May() { X = move[2], Y = move[3] });
                        }
                    }
                }

            }
            return chess;
        }


        public static bool computer_flag = false;
        [HttpPost("Computer")]
        /// <summary>
        /// 电脑出招
        /// </summary>
        /// <returns></returns>
        public async Task<Chess> Computer()
        {
            if (computer_flag)
            {
                return null;
            }
            computer_flag = true;

            if (!Continue)
            {
                computer_flag = false;
                return null;
            }

            //玩家回合不响应请求
            if (chess.Count % 2 == 0 && playerRed || chess.Count % 2 == 1 && !playerRed)
            {
                computer_flag = false;
                return null;
            }

            chess.ComputerMove();
            if (chess.AllMoves().Count == 0)
            {
                Continue = false;
            }
            computer_flag = false;
            return chess;
        }
    }

    public class Point
    {
        public int X { set; get; }
        public int Y { set; get; }
    }
}
